// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let id= event.id
  let limit = event.limit
  let nowPage = event.nowPage
  let newData = []
  // return event

  let data = await db.collection('zanmei').skip(nowPage * limit).limit(limit).where({
        shijiIndex:id
      }).orderBy('xuhao','desc').get({
      success: function (res) {

        return res
      }
    })
  
  data = data.data

  for(var i = 0 ; i < data.length; i++){

    if(id == 0){
      var xuhao = 'f' + data[i]['xuhao'] + data[i]['mulu']
    }else{

      var xuhao =  data[i]['xuhao'] + data[i]['mulu']
    }

    newData[i] = {serialNum:data[i]['xuhao'],name:data[i]['mulu'],id: xuhao,bz:data[i]['bz'],jc:data[i]['jc']}

  }
  
  let count =await db.collection('zanmei').where({
        shijiIndex: id
      }).count({
            success: function (res) {
              return res
            }
      })

  let page = Math.ceil( count.total/ limit) 

  return {newData,count,limit,nowPage}



}